import { ComputedRef } from 'vue';
/* model */
import { BaseSelectUserVuexPropertyEnum } from '@src/component/common/BaseSelectUser/store'
/* store */
import { useBaseSelectUserStore } from '@src/component/common/BaseSelectUser/store/hooks/base'
/* vue */
import { computed } from 'vue'
/* type */
import { BaseSelectUserSelectedItemType } from '@src/component/common/BaseSelectUser/types'
/* util */
import { getCheckedItemId } from '@src/component/common/BaseSelectUser/util'

/** 
 * @description 选人控件 已选中列表
*/
function useBaseSelectUserStoreCheckedItemList() {
  
  const store = useBaseSelectUserStore()
  
  // 已选择数据项列表
  const checkedItemList = computed(() => store.state.checkedItemList)
  
  /** 
   * @description 已选择数据项列表对象
   * 以每一项的 id 为 key，每一项为 value。
   * @example { [每一项的id]: 每一项 }
  */
  const checkedItemListIdMap: ComputedRef<Record<string, BaseSelectUserSelectedItemType>> = computed(() => {
    return (
      checkedItemList.value.reduce((acc, item) => {
        acc[getCheckedItemId(item)] = item
        return acc
      }, {} as Record<string, BaseSelectUserSelectedItemType>)
    )
  })
  
  const setCheckedItemList = (value: BaseSelectUserSelectedItemType[]) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetCheckedItemList, value)
  }
  
  return {
    checkedItemList,
    checkedItemListIdMap,
    setCheckedItemList
  }
}

export {
  useBaseSelectUserStoreCheckedItemList
}
